﻿@using IOA.MES.DataAccess.Enum
@using IOA.MES.DataAccess.VModel
@{
    ViewBag.Title = "用户管理";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}
@section Contents{
    <style>
        .layui-form-label {
            width: auto;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.Department}}：</label>
                        <div class="layui-input-inline">
                            <select id="selDepartments" lay-filter="selDepartments">
                                <option value="">所有部门</option>
                                @foreach (var item in ViewBag.Departments as List<VDepartment>)
                                {
                                    <option value="@item.PkId">@item.DepartmentName</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.Role}}：</label>
                        <div class="layui-input-inline">
                            <select id="selRoles" lay-filter="selRoles">
                                <option value="">所有岗位</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.WorkShop}}：</label>
                        <div class="layui-input-inline">
                            <select id="selWorkshops" lay-filter="selWorkshops">
                                <option value="">所有车间</option>
                                @foreach (var item in ViewBag.Workshops as List<VWorkshop>)
                                {
                                    <option value="@item.WorkshopCode">@item.WorkshopName</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.Status}}：</label>
                        <div class="layui-input-inline">
                            <select id="selStatus" lay-filter="selStatus">
                                <option value="">所有状态</option>
                                <option value="@((int)CommonStatus.Disabled)">禁用</option>
                                <option value="@((int)CommonStatus.Normal)">正常</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.Keyword}}：</label>
                        <div class="layui-input-inline" style="width: 240px;">
                            <input type="text" id="txtKey" placeholder="如登录名、姓名、手机号、邮箱等" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn layuiadmin-btn-list" type="button">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                        <button class="layui-btn layui-btn-normal layuiadmin-btn-list" type="button" onclick="editUser(0);">
                            <i class="layui-icon layui-icon-addition layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });

        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['table', 'layer', 'form'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    form = layui.form;

                var options = {
                    elem: '#table'
                    , url: '/Settings/QueryUsers'
                    , method: "post"
                    , where: { departmentId: 0, roleId: 0, workshopCode: "", status: null, key: '' }
                    , page: {
                        limit: 15
                        , limits: [15, 20, 25, 30, 40, 50]
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers", width: 60, fixed: "left" }
                        , { field: 'UserName', minWidth: 100, title: '登录名', fixed: "left" }
                        , { field: 'TrueName', minWidth: 100, title: '真实姓名', fixed: "left" }
                        , {
                            title: '部门岗位', minWidth: 220, templet: function (d) {
                                var roles = new Array();
                                for (var i = 0; i < d.Roles.length; i++) {
                                    roles.push(d.Roles[i].DepartmentName + "：" + d.Roles[i].RoleName);
                                }
                                return roles.join("<br/>");
                            }
                        }
                        , {
                            title: '车间', minWidth: 100, templet: function (d) {
                                var workshops = new Array();
                                for (var i = 0; i < d.Workshops.length; i++) {
                                    workshops.push(d.Workshops[i].WorkshopName);
                                }
                                return workshops.join("、");
                            }
                        }
                        , { field: 'Phone', minWidth: 100, title: '手机号' }
                        , { field: 'Email', minWidth: 100, title: '邮箱' }
                        , { field: 'CreateTimeStr', title: '创建时间', minWidth: 150 }
                        , { field: 'CNStatus', minWidth: 80, title: '状态' }
                        , {
                            title: '操作', minWidth: 380, fixed: "right", templet: function (d) {
                                var str = '<button class="layui-btn layui-btn-sm layui-btn-normal" onclick="editUser(' + d.PkId + ')"><i class="layui-icon">&#xe642;</i>编辑</button>\
                                           <button class="layui-btn layui-btn-sm" onclick="resetPassword(' + d.PkId + ')"><i class="layui-icon">&#xe683;</i>重置密码</button>\
                                           <button class="layui-btn layui-btn-sm" onclick="modifyUserStatus(' + d.PkId + ',' + (1 - d.Status) + ')"><i class="layui-icon">&#xe616;</i>' + (d.Status == 0 ? "启用" : "禁用") + '</button>\
                                           <button class="layui-btn layui-btn-sm layui-btn-danger" onclick="deleteUser(' + d.PkId + ',\'' + d.TrueName + '\')"><i class="layui-icon">&#xe640;</i>删除</button>';
                                return str;
                            }
                        }
                    ]]
                };

                $("#btnSearch").click(function () {
                    queryTable();
                });
                queryTable();
                function queryTable() {
                    options.page.curr = 1;
                    options.where.departmentId = $("#selDepartments").val().trim() == "" ? 0 : parseInt($("#selDepartments").val().trim());
                    options.where.roleId = $("#selRoles").val().trim() == "" ? 0 : parseInt($("#selRoles").val().trim());
                    options.where.workshopCode = $("#selWorkshops").val();
                    options.where.status = $("#selStatus").val().trim() == "" ? null : parseInt($("#selStatus").val().trim());
                    options.where.key = $("#txtKey").val();

                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }

                var departmentRoles = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(ViewBag.Departments));
                form.on('select(selDepartments)', function (data) {
                    var exist = departmentRoles.find(function (e) {
                        return e.PkId == parseInt(data.value);
                    });
                    var options = ["<option value=\"\">全部岗位</option>"];
                    if (parseInt(data.value) > 0) {
                        for (var j = 0; j < exist.Roles.length; j++) {
                            options.push("<option value=" + exist.Roles[j].PkId + ">" + exist.Roles[j].RoleName + "</option>");
                        }
                    }
                    $("#selRoles").html(options.join(""));
                    form.render("select");
                });
            });
        }

        function editUser(userId) {
            layer.open({
                type: 2,
                shade: [0.1, '#000'],
                area: ['550px', '700px'],
                content: "/Settings/EditUser?userId=" + userId,
                end: function () {
                    $("#btnSearch").click();
                }
            });
        }

        function deleteUser(userId, trueName) {
            layer.confirm('确定要删除“' + trueName + '”吗？', { icon: 3, title: '提示' }, function (index) {
                var loadId = layer.load();
                $.ajax({
                    url: "/Settings/DeleteUser",
                    data: {
                        userId: userId
                    },
                    complete: function () {
                        layer.close(loadId);
                    },
                    success: function (res) {
                        if (res.ErrCode != 200) {
                            layer.msg(res.ErrMsg, { icon: 5 });
                            return;
                        }
                        layer.msg("删除成功！", { icon: 6, time: 1500 });
                        $("#btnSearch").click();
                    }
                });
                layer.close(index);
            });
        }

        function resetPassword(userId) {
            layer.confirm('确定要重置密码吗？重置后密码将恢复到：123456', { icon: 3, title: '提示' }, function (index) {
                var loadId = layer.load();
                $.ajax({
                    url: "/Settings/ResetPassword",
                    data: {
                        userId: userId
                    },
                    complete: function () {
                        layer.close(loadId);
                    },
                    success: function (res) {
                        if (res.ErrCode != 200) {
                            layer.msg(res.ErrMsg, { icon: 5 });
                            return;
                        }
                        layer.msg("重置成功！", { icon: 6, time: 1500 });
                    }
                });
                layer.close(index);
            });
        }

        function modifyUserStatus(userId, status) {
            var loadId = layer.load();
            $.ajax({
                url: "/Settings/ModifyUserStatus",
                data: {
                    userId: userId,
                    status: status
                },
                complete: function () {
                    layer.close(loadId);
                },
                success: function (res) {
                    if (res.ErrCode != 200) {
                        layer.msg(res.ErrMsg, { icon: 5 });
                        return;
                    }
                    layer.msg("修改成功！", { icon: 6, time: 1500 });
                    $("#btnSearch").click();
                }
            });
            layer.close(index);
        }
    </script>
}
